package com.abcdefg.webpagechatprojectbymyself.api;

import com.abcdefg.webpagechatprojectbymyself.mapper.MessageMapper;
import com.abcdefg.webpagechatprojectbymyself.mapper.MessageSessionMapper;
import com.abcdefg.webpagechatprojectbymyself.model.Friend;
import com.abcdefg.webpagechatprojectbymyself.model.MessageSession;
import com.abcdefg.webpagechatprojectbymyself.model.MessageSessionUser;
import com.abcdefg.webpagechatprojectbymyself.model.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("MessageSessionApi")
public class MessageSessionApi {

    @Autowired
    private MessageSessionMapper messageSessionMapper;
    @Autowired
    private MessageMapper messageMapper;

    @RequestMapping("/sessionList")
    public Object getListSession(HttpServletRequest request){
        //用来保存全部会话消息
        ArrayList<MessageSession> sessionList = new ArrayList<>();
        HttpSession session = request.getSession();
        if(session == null){
            System.out.println("当前获取不到session对象！！！");
            return sessionList;
        }
        User user=(User)session.getAttribute("user");
        if(user == null){
            System.out.println("当前获取不到user对象");
            return sessionList;
        }
        List<Integer> sessionListById=
                messageSessionMapper.getSessionIdByUserId(user.getUserId());
        for(Integer sessionId:sessionListById){
            //创建MessageSession用来保存每一个会话下的信息
            MessageSession messageSession=new MessageSession();
            messageSession.setSessionId(sessionId);
            List<Friend> friendList=
                    messageSessionMapper.getFriendBySessionId(sessionId, user.getUserId());
            messageSession.setFriendList(friendList);
            String lastContent=messageMapper.getLastMessageBySessionId(sessionId);
            if(lastContent != null){
                messageSession.setLastMessage(lastContent);
            }else{
                messageSession.setLastMessage("");
            }
            sessionList.add(messageSession);
        }
        return sessionList;
    }


    //添加好友列表中的会话
    @RequestMapping("/session")
    @Transactional
    public Object addMessageSession(int toUserId, @SessionAttribute("user")User user){
        //返回接口设置为session: 1
        HashMap<String,Integer> map=new HashMap<>();
        MessageSession messageSession=new MessageSession();
        //通过addMessageSession方法生成的sessionId和lastTime放到messageSession对象中
        messageSessionMapper.addMessageSession(messageSession);
        MessageSessionUser user1=new MessageSessionUser();
        user1.setUserId(toUserId);
        user1.setSessionId(messageSession.getSessionId());
        messageSessionMapper.addMessageSessionUser(user1);
        MessageSessionUser user2=new MessageSessionUser();
        user2.setUserId(user.getUserId());
        user2.setSessionId(messageSession.getSessionId());
        messageSessionMapper.addMessageSessionUser(user2);
        map.put("sessionId",messageSession.getSessionId());
        return map;
    }
}
